<?php
include('../conn.php');

/**
 *  过期积分返券金额，返回给厂商
 */
class Auto_integral_coupon {
	public function auto() {
		$now_time = date('Y-m-d H:i:s');
		mysql_query("BEGIN");
		$sql = "SELECT shou_huo_datetime, ordersid, shopid FROM goodsorders WHERE state = 5"  . " AND fan_juan_price != 0.00 AND state = 3";
		$orders = mysql_query($sql);
		while ($row = mysql_fetch_array($orders)) {
		$time = date('Y-m-d H:i:s',strtotime("+7 day",strtotime($row['shou_huo_datetime'])));
			if($now_time >= $time) {
				$sql = "SELECT a.fan_juan_price, a.shopid, b.auto_integral_coupon, b.balance FROM goodsorders AS a 
                                LEFT JOIN shop AS b ON a.shopid = b.shopid
                                WHERE a.ordersid = " . $row['ordersid'];
				$order = mysql_fetch_assoc(mysql_query($sql));
				$price = $order['fan_juan_price']+$order['auto_integral_coupon'];
				$balance = $order['balance']-$order['fan_juan_price'];
				$sql = "UPDATE shop SET auto_integral_coupon = $price, balance = $balance WHERE shopid = " . $row['shopid'];
				$res = mysql_query($sql);
				$sql = "UPDATE goodsorders SET fan_juan_price = 0 WHERE ordersid = " . $row['ordersid'];
				mysql_query($sql);
			}
		}
		mysql_query("COMMIT"); 
		mysql_query("END");
	}
}

$orders = new Auto_integral_coupon();
$orders->auto();
